/*
 * @lc app=leetcode.cn id=504 lang=cpp
 *
 * [504] 七进制数
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string convertToBase7(int num)
    {
        string ans("");
        if (num < 0)
        {
            ans += '-';
            num = -num;
        }
        if(num < 7)
        {
            ans += to_string(num);
            return ans;
        }

        int exp = log(num) / log(7);

        vector<int> v;
        v.push_back(1);
        for (int i = 1; i <= exp; i++)
            v.push_back(v[i - 1] * 7);

        for (int i = exp; i >= 0; --i)
        {
            ans += to_string(num / v[i]);
            num %= v[i];
        }

        return ans;
    }
};
// @lc code=end
